package cn.yangliu.jwt.web;

import cn.yangliu.jwt.comm.JsonResult;
import cn.yangliu.jwt.entity.User;
import cn.yangliu.jwt.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 杨柳
 * @contact QQ: 574789490
 * @date 2019-04-18 10:38
 */
@Api(value = "User操作接口", tags = "User操作接口", description = "User操作接口")
@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {
    
    @Autowired
    private UserService userService;


    
    /**
     * 新增或修改
     */
    @PostMapping
    @ApiOperation(value = "新增或修改User", httpMethod = "POST", notes = "新增或修改User", tags = "User操作接口", response = JsonResult.class)
    public JsonResult save(@RequestBody User data){
        userService.save(data);

        return JsonResult.getInstance();
    }

    /**
     * 列表查询
     */
    @GetMapping
    @ApiOperation(value = "列表查询User", httpMethod = "GET", notes = "列表查询User", tags = "User操作接口", response = JsonResult.class)
    public JsonResult findAll(@RequestParam User queryPojo, int page, int size){
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<User> data = userService.findAll(queryPojo, pageRequest);

        return JsonResult.getInstance(data);
    }

    /**
     * 查询单个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过主键查询User", httpMethod = "GET", notes = "通过主键查询User", tags = "User操作接口", response = JsonResult.class)
    public JsonResult findById(@PathVariable("id") Long id){
        User data = userService.findById(id).orElse(null);

        return JsonResult.getInstance(data);
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "通过主键删除User", httpMethod = "DELETE", notes = "通过主键删除User", tags = "User操作接口", response = JsonResult.class)
    public JsonResult deleteById(@RequestBody Long id){
        userService.deleteById(id);

        return JsonResult.getInstance();
    }


}
